{% assign id = include.map-id %}
{% assign data = site.data.maps-vector[id] %}
{% assign color = include.color | default: data.color | default: 'green' %}

{% if data %}
	<div class="ratio ratio-21x9">
		<div>
			<div id="map-{{ id }}" class="w-100 h-100"></div>
		</div>
	</div>

	{% capture script %}
	<script>
		// @formatter:on
		{% if jekyll.environment == 'development' %}window.tabler_map_vector = window.tabler_map_vector || {};{% endif %}

		document.addEventListener("DOMContentLoaded", function() {
			const map = {% if jekyll.environment == 'development' %}window.tabler_map_vector["map-{{ id }}"] = {% endif %}new jsVectorMap({
				selector: '#map-{{ id }}',
				map: '{{ data.map }}',
				backgroundColor: 'transparent',
				regionStyle: {
					initial: {
						{% unless data.filled %}
						fill: '#F8FAFC',
						stroke: 'rgba(98, 105, 118, .16)',
						strokeWidth: 1,
						{% else %}
						fill: '#e9ecef',
						stroke: '#fff',
						strokeWidth: 1,
						{% endunless %}
					}
				},
				zoomOnScroll: {% if data.zoom %}true{% else %}false{% endif %},
				zoomButtons: {% if data.zoom %}true{% else %}false{% endif %},
				{% if data.values %}
				// -------- Series --------
				visualizeData: {
					scale: ['#F8FAFC', '{{ data.color | tabler_color }}'],
					values: {{ data.values }},
				},
				{% endif %}
				{% if data.markers %}
				markers: [
					{% for marker in data.markers %}
					{
						coords: [{{ marker.coords }}],
						name: "{{ marker.name }}",
					},
					{% endfor %}
				],
				markerStyle: {
					initial: {
						r: 3,
						stroke: '#fff',
						opacity: 1,
						strokeWidth: 3,
						stokeOpacity: .5,
						fill: '{{ color | tabler_color }}'
					},
					hover: {
						fill: '{{ color | tabler_color }}',
						stroke: '{{ color | tabler_color }}'
					}
				},
				markerLabelStyle: {
					initial: {
						fontSize: 10
					},
				},
				labels: {
					markers: {
						render: function(marker) {
							return marker.name
						},
					},
				},
				{% endif %}
			});

			window.addEventListener("resize", () => {
				map.updateSize();
			});
		});
		// @formatter:off
	</script>
	{% endcapture %}

	{% if include.show-scripts %}
		{{ script }}
	{% else %}
		{% capture_global scripts %}
			{{ script }}
		{% endcapture_global %}
	{% endif %}
{% endif %}
